<?php
	require '../include/library.inc.php';
	require '../include/admin_login.inc.php';

$course_id = intval(($_GET['course_id'])?$_GET['course_id']:$_POST['course_id']);
if (!$course_id) redirect("schools.php");
	
# Ако запазваме	
if ($_POST['is_submitted']) {
    $error_message = '';
    
	# Проверки
	if (!$_POST['name'])  $error_message .= 'Course Name is required<br/>';	
	if (!$_POST['description'])  $error_message .= 'Description is required<br/>';
	if (!$_POST['course_type_id'])  $error_message .= 'Type is required<br/>';
    
	# OK
    if(!$error_message) {
				
		# Create the course
		$sql = 'UPDATE courses
				SET
					course_type_id = '.intval($_POST['course_type_id']).', 
					name = \''.escape($_POST['name']).'\', 
					description = \''.escape($_POST['description']).'\', 
					registration_fee = '.floatval($_POST['registration_fee']).'
				WHERE
					course_id = '.$course_id.'
				  ';
		query($sql);
	  			
		$_SESSION['message'] = 'Course '.$_POST['name'].' was updated!';
		redirect("school_edit.php?school_id=".$_POST['school_id']."#courses");
	  
    } else { //Има грешка
		
		$row_course =& $_POST;
		
	}
	
} else { //Първоначално зареждане
	
	$sql = 'SELECT
			  *
			FROM
			  courses
			WHERE
			  course_id = '.$course_id.'
		';
	$row_course = mysql_fetch_assoc(query($sql));
	
	if(!$row_course['course_id']) redirect("schools.php");
	
}

head_admin("Edit Course");
?>
<script type="text/javascript" src="<?=HTML_BASE_ADMIN?>tiny_mce/tiny_mce.js"></script>
<script type="text/javascript">
tinyMCE.init({
	// General options
	mode : "exact",
    elements : "body",
	theme : "advanced",
	relative_urls : false,
	plugins : "safari,spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,imagemanager",

	// Theme options
	theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,formatselect,fontselect,fontsizeselect",
	theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
	theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen,|,image,insertimage",
	theme_advanced_toolbar_location : "top",
	theme_advanced_toolbar_align : "left",
	theme_advanced_statusbar_location : "bottom",
	theme_advanced_resizing : false,

	// Drop lists for link/image/media/template dialogs
	//template_external_list_url : "js/template_list.js",
	external_link_list_url : "js/link_list.php",
	//media_external_list_url : "js/media_list.js",
	//external_image_list_url : "js/image_list.php"
	
});
</script>
		
		<?php if ($error_message) { ?>
		  <div class="message red">
			<h4>Error!</h4>
			<?=$error_message?>
		  </div>
		<?php } ?>
		
		
		
            <div class="topbuttons">
            	<a href="school_edit.php?school_id=<?=$row_course['school_id']?>#courses">Back to school edit</a>
            </div>
            
            <h3>Edit Course from <?=$_GET['school_name']?></h3>
            
            <form action="<?=$_SERVER['PHP_SELF']?>" method="post" enctype="multipart/form-data">
            <input type="hidden" name="is_submitted" value="1" />
            <input type="hidden" name="course_id" value="<?=$course_id?>" />
            <input type="hidden" name="school_id" value="<?=$row_course['school_id']?>" />
            
			<dl class="clearfix">
			
			<dt><label class="required">Course Name</label></dt>
            <dd><input class="inputbox w500" name="name" type="text" value="<?=sth($row_course['name'])?>" ></dd>            
						
            <dt><label class="required">Description</label></dt>
            <dd><textarea id="body" name="description" rows="24" style="width:100%" ><?=sth($row_course['description'])?></textarea></dd>
			                        
            <dt><label class="required">Type</label></dt>
			<dd><select class="inputbox w250" name="course_type_id">
            <option value="">Choose type</option>
            <?php
			$sql = "SELECT
						course_type_id,
						name
					FROM
						course_types
					  ";
			$result = query($sql);
            while ($row_type = mysql_fetch_object($result)) {
				echo '<option value="'.$row_type->course_type_id.'" '.(($row_course['course_type_id']==$row_type->course_type_id)?' selected':'').'>'.$row_type->name.'</option>';
			}
			?>
            </select></dd>
            
            <dd>
            Application fee $
            <input class="inputbox" style="width:120px;" id="lng" name="registration_fee" type="text" value="<?=$row_course['registration_fee']?>" maxlength="20" />
            </dd>
            
            </dl>
            
            <a name="dates"></a>
            <div class="topbuttons">
            	<a class="add" href="course_dates.php?course_id=<?=$course_id?>&school_name=<?=$_GET['school_name']?>">Manage dates</a>
            </div>
            
            <h4>Dates &amp; Durations</h4>
            <dl>
            <?php
			$sql = "SELECT
						start_date,
						duration,
						tuition
					FROM
						course_dates
					WHERE
						course_id = ".$course_id."
					ORDER BY
						start_date
					  ";
			$rs_date = query($sql);
			while ($row_dates = mysql_fetch_object($rs_date)) {
				echo $row_dates->start_date.', '.$row_dates->duration.' weeks, $'.$row_dates->tuition.' <br/>';
			}
			?>
            </dl>
            
            </dl>                      
			<dd class="submit">
            <input type="submit" name="submit" id="submit" value="Save">
            </dd>
			
            </dl>
			</form>
            
<?php
	foot_admin(); 
?>